<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/5/29
 * Time: 11:24
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Report extends Base {
    public function index(){
        if(request()->isAjax()){
            $map = array();
            extract(input());
            if(isset($keyword)&&$keyword!="")
            {
                $map['a.topic_title|a.topic_content|uuu.user_username|uuu.user_phone'] = ['like',"%" . $keyword . "%"];
            }
            if(isset($province)&&$province!="")
            {
                $map['a.topic_is_show'] = $province;
            }
            if(isset($topic_type)&&$topic_type!="")
            {
                $map['a.topic_type'] = $topic_type;
            }
            if(isset($start) && $start != '' && isset($end) && $end != ''){
                $map['topic_fb_time'] = ['between', [$start, $end]];
            }
            if(isset($start) && $start != '' && isset($end) && $end == ''){
                $map['topic_fb_time'] = ['> time', $start];
            }
            if(isset($start) && $start == '' && isset($end) && $end != ''){
                $map['topic_fb_time'] = ['< time', $end];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('topic_report')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.r_user_id','left')
                ->join('up_topic_report_type utrt','utrt.report_type_id = a.report_type_id','left')
                ->join('up_topic ut','ut.topic_id =a.topic_id','left')
                ->where($map)->count();// 获取总条数
            $lists = Db::name('topic_report')->alias('a')
                ->where($map)
                ->join('up_u_user uuu','uuu.user_id = a.r_user_id','left')
                ->join('up_topic_report_type utrt','utrt.report_type_id = a.report_type_id','left')
                ->join('up_topic ut','ut.topic_id =a.topic_id','left')
                ->page($NowPage,$limits)
                ->field('a.*,uuu.user_username,uuu.user_phone,ut.topic_is_show,utrt.report_type_name')
                ->order('a.report_time desc')
                ->select();
            foreach ($lists as $key=>$vo){

                if($vo['report_img'] != ''){
                    $interlocution_img = trim($vo['report_img'],',');
                    $photo = explode (',',$interlocution_img);
                    $lists[$key]['picture'] = $photo[0];
                    unset($photo[0]);
                    $lists[$key]['report_img'] = array_values($photo);
                }else{
                    $lists[$key]['picture'] = '';
                    $lists[$key]['report_img']= '';
                }
            }

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $where['report_type_status'] =1;
        $where['is_del'] =1;
        $topic_type = Db::table('up_topic_report_type')->where($where)->select();
        $this->assign('report_type',$topic_type);
        return $this->fetch();
    }

    /**
     * 标记已处理
     * @return \think\response\Json
     */
    public function handle(){
        $yy_id = input('id');
        $res = Db::table('up_topic_report')
            ->where('report_id', $yy_id)
            ->update(['report_status' => 2]);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    /***
     * @return \think\response\Json
     * 下架
     */
    public function down(){
        $task_id = input('param.id');
        $res = Db::name('topic')->where('topic_id',$task_id)->setField('topic_is_show',2);
        if($res){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>100,'msg'=>'操作失败']);
        }
    }
}